package com.sec.samsung.android.app.baymax.view.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sec.samsung.android.app.baymax.R;
import com.sec.samsung.android.app.baymax.log.AIOLog;
import com.sec.samsung.android.app.baymax.model.Question;
import com.sec.samsung.android.app.baymax.model.reponse.ResponseListQuestionData;
import com.sec.samsung.android.app.baymax.utils.BaymaxConstant;
import com.sec.samsung.android.app.baymax.utils.StringUtility;
import com.sec.samsung.android.app.baymax.view.adapter.ListGridviewAdapter;
import com.sec.samsung.android.app.baymax.view.adapter.ListGridviewAdapter.OnCheckSingleImageItemListener;
import com.sec.samsung.android.app.baymax.view.adapter.MultiChoiceAdapter;
import com.sec.samsung.android.app.baymax.view.adapter.MultiChoiceAdapter.OnCheckItemMultiListener;
import com.sec.samsung.android.app.baymax.view.adapter.SingleChoiceAdapter;
import com.sec.samsung.android.app.baymax.view.adapter.SingleChoiceAdapter.OnCheckSingleItemListener;

public class DetailSymptomQuestions extends Activity implements OnCheckItemMultiListener, OnCheckSingleItemListener, OnCheckSingleImageItemListener {
    private Button mBtnBack;
    private LinearLayout mLayoutLoading;
    private LinearLayout mLayoutNoConnection;
    private LinearLayout mBtnStartOver;
    private LinearLayout mLayoutYesNoAnswer;
    private LinearLayout mLayoutMultiChoiceAnswer;
    private LinearLayout mLayoutSingleChoiceAnswer;
    private LinearLayout mLayoutImageSingleChoiceAnswer;
    private TextView mTxtQuestion;
    private ListView mListMultiChoice;
    private ListView mListSingnleChoice;
    private GridView mGridViewImage;

    private Button mBtnBackAnswer;
    private Button mBtnYesAnswer;
    private Button mBtnNoAnswer;
    private MultiChoiceAdapter mMultiChoiceAdapter;
    private SingleChoiceAdapter mSingleChoiceAdapter;
    private ListGridviewAdapter mListGridviewAdapter;

    private ArrayList<Question> mListQuestions;
    private int mCurrentQuestion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_symptom);

        initLayout();

        initData();
    }

    private void initData() {

        String symptomsQuestions = getIntent().getExtras().getString("symptoms_question");
        Gson gson = new GsonBuilder().serializeNulls().create();
        if (StringUtility.isEmpty(symptomsQuestions))
            finish();
        ResponseListQuestionData responseData = gson.fromJson(symptomsQuestions, ResponseListQuestionData.class);
        if (responseData == null)
            return;
        mListQuestions = responseData.value;
        mCurrentQuestion = 0;

        if (mListQuestions == null || mListQuestions.size() == 0)
            finish();
        else
            loadQuestionLayout(mCurrentQuestion);
    }

    private void loadQuestionLayout(int currentQuestion) {
        Question question = mListQuestions.get(currentQuestion);
        mTxtQuestion.setText(question.getQuestion());
        AIOLog.d("question.getType():" + question.getType());

        if (BaymaxConstant.TYPE_YES_NO.equalsIgnoreCase(question.getType())) {
            mLayoutYesNoAnswer.setVisibility(View.VISIBLE);
            mLayoutMultiChoiceAnswer.setVisibility(View.GONE);
            mLayoutSingleChoiceAnswer.setVisibility(View.GONE);
        } else if (BaymaxConstant.TYPE_SELECT_MULTI.equalsIgnoreCase(question.getType())) {
            mLayoutYesNoAnswer.setVisibility(View.GONE);
            mLayoutMultiChoiceAnswer.setVisibility(View.VISIBLE);
            if (!question.isCheckImageQuestion()) {
                mLayoutSingleChoiceAnswer.setVisibility(View.GONE);
                mLayoutImageSingleChoiceAnswer.setVisibility(View.GONE);

                mMultiChoiceAdapter = new MultiChoiceAdapter(this, question);
                mMultiChoiceAdapter.setLisntener(this);
                mListMultiChoice.setAdapter(mMultiChoiceAdapter);
                mListMultiChoice.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
            } else {
                mLayoutSingleChoiceAnswer.setVisibility(View.GONE);
                mLayoutImageSingleChoiceAnswer.setVisibility(View.VISIBLE);

                mListGridviewAdapter = new ListGridviewAdapter(this, question);
                mListGridviewAdapter.setLisntener(this);
                mGridViewImage.setAdapter(mListGridviewAdapter);
                mGridViewImage.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE);
            }
        } else if (BaymaxConstant.TYPE_SELECT_SINGLE.equalsIgnoreCase(question.getType())) {
            mLayoutYesNoAnswer.setVisibility(View.GONE);
            mLayoutMultiChoiceAnswer.setVisibility(View.GONE);
            if (!question.isCheckImageQuestion()) {
                mLayoutSingleChoiceAnswer.setVisibility(View.VISIBLE);
                mLayoutImageSingleChoiceAnswer.setVisibility(View.GONE);

                mSingleChoiceAdapter = new SingleChoiceAdapter(this, question);
                mSingleChoiceAdapter.setLisntener(this);
                mListSingnleChoice.setAdapter(mSingleChoiceAdapter);
                mListSingnleChoice.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            } else {
                mLayoutSingleChoiceAnswer.setVisibility(View.GONE);
                mLayoutImageSingleChoiceAnswer.setVisibility(View.VISIBLE);

                mListGridviewAdapter = new ListGridviewAdapter(this, question);
                mListGridviewAdapter.setLisntener(this);
                mGridViewImage.setAdapter(mListGridviewAdapter);
                mGridViewImage.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE);
            }
        }
    }

    private void initLayout() {
        mBtnBack = (Button) findViewById(R.id.btn_back);
        mLayoutLoading = (LinearLayout) findViewById(R.id.layout_loading);
        mLayoutNoConnection = (LinearLayout) findViewById(R.id.layout_no_connection);
        mLayoutMultiChoiceAnswer = (LinearLayout) findViewById(R.id.layout_content_multi_choice_answer);
        mLayoutSingleChoiceAnswer = (LinearLayout) findViewById(R.id.layout_content_single_choice_answer);
        mLayoutYesNoAnswer = (LinearLayout) findViewById(R.id.layout_content_yes_no_question);
        mLayoutImageSingleChoiceAnswer = (LinearLayout) findViewById(R.id.layout_content_single_choice_image);

        mTxtQuestion = (TextView) findViewById(R.id.txt_question_content);
        mBtnStartOver = (LinearLayout) findViewById(R.id.layout_start_over);

        mListMultiChoice = (ListView) findViewById(R.id.list_multi_choice);
        mListSingnleChoice = (ListView) findViewById(R.id.list_single_choice);
        mGridViewImage = (GridView) findViewById(R.id.grid_single_choice_image);

        mListSingnleChoice.setAdapter(mSingleChoiceAdapter);

        mBtnStartOver.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DetailSymptomQuestions.this, ProfileActivity.class);
                startActivity(intent);
                finish();
            }
        });

        mBtnBack.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mBtnBackAnswer = (Button) findViewById(R.id.btn_back_confirm);
        mBtnYesAnswer = (Button) findViewById(R.id.btn_yes_confirm);
        mBtnNoAnswer = (Button) findViewById(R.id.btn_no_confirm);
        mBtnBackAnswer.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (mCurrentQuestion == 0) {
                    finish();
                }
                else {
                    mCurrentQuestion--;
                    loadQuestionLayout(mCurrentQuestion);
                }
            }
        });
        mBtnYesAnswer.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d("KieuThang", "mCurrentQuestion:" + mCurrentQuestion + ", mListQuestions.size():" + mListQuestions.size());
                ArrayList<String> answers = new ArrayList<String>();
                answers.add("yes");
                mListQuestions.get(mCurrentQuestion).setAnswers(answers);
                if (mCurrentQuestion < mListQuestions.size() - 1) {
                    mCurrentQuestion++;
                    loadQuestionLayout(mCurrentQuestion);
                } else {
                    Intent intent = new Intent(DetailSymptomQuestions.this, ResultActivity.class);
                    Gson gson = new GsonBuilder().serializeNulls().create();
                    String answerData = gson.toJson(mListQuestions);
                    intent.putExtra("answer_data", answerData);
                    startActivity(intent);
                }
            }
        });
        mBtnNoAnswer.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ArrayList<String> answers = new ArrayList<String>();
                answers.add("no");
                mListQuestions.get(mCurrentQuestion).setAnswers(answers);
                if (mCurrentQuestion < mListQuestions.size() - 1) {
                    mCurrentQuestion++;
                    loadQuestionLayout(mCurrentQuestion);
                } else {
                    Intent intent = new Intent(DetailSymptomQuestions.this, ResultActivity.class);
                    Gson gson = new GsonBuilder().serializeNulls().create();
                    String answerData = gson.toJson(mListQuestions);
                    intent.putExtra("answer_data", answerData);
                    startActivity(intent);
                }
            }
        });
    }

    public void onClickBackSingle(View v) {
        if (mCurrentQuestion == 0) {
            finish();
        }
        else {
            mCurrentQuestion--;
            loadQuestionLayout(mCurrentQuestion);
        }
    }

    public void onClickNextSingle(View v) {
        ArrayList<String> answers = new ArrayList<String>();
        answers.add("no");
        mListQuestions.get(mCurrentQuestion).setAnswers(answers);
        if (mCurrentQuestion < mListQuestions.size() - 1) {
            mCurrentQuestion++;
            loadQuestionLayout(mCurrentQuestion);
        } else {
            Intent intent = new Intent(DetailSymptomQuestions.this, ResultActivity.class);
            Gson gson = new GsonBuilder().serializeNulls().create();
            String answerData = gson.toJson(mListQuestions);
            intent.putExtra("answer_data", answerData);
            startActivity(intent);
        }
    }

    public void onClickNextMulti(View v) {
        ArrayList<String> answers = new ArrayList<String>();
        answers.add("no");
        mListQuestions.get(mCurrentQuestion).setAnswers(answers);
        if (mCurrentQuestion < mListQuestions.size() - 1) {
            mCurrentQuestion++;
            loadQuestionLayout(mCurrentQuestion);
        } else {
            Intent intent = new Intent(DetailSymptomQuestions.this, ResultActivity.class);
            Gson gson = new GsonBuilder().serializeNulls().create();
            String answerData = gson.toJson(mListQuestions);
            intent.putExtra("answer_data", answerData);
            startActivity(intent);
        }
    }

    public void onClickBackMulti(View v) {
        if (mCurrentQuestion == 0) {
            finish();
        }
        else {
            mCurrentQuestion--;
            loadQuestionLayout(mCurrentQuestion);
        }
    }

    @Override
    public void onCheckItemSingleLisntener(int position, boolean value) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onCheckItemMultiLisntener(int position, boolean value) {
        // TODO Auto-generated method stub

    }
}
